<template>
    <div>

        <el-form ref="form" :model="form" label-width="120px">
            <el-form-item label="用户名">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model.trim="form.password" placeholder="请输入密码" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认密码">
                <el-input v-model.trim="form.checkPass" placeholder="请确认密码" show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :plain="true" @click="onRegister">注册</el-button>
                <el-button type="primary" @click="onReset">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { Message } from "element-ui";
import { req_register } from "@/api/registerLogin.js";
export default {
    data() {
        return {
            form: {
                name: '',
                password: '',
                checkPass: '',
            },
        };

    },
    methods: {
        async onRegister() {
            // console.log(
            //     this.form.name,
            //     this.form.password,
            //     this.form.checkPass,
            // );
            // 发送Ajax请求
            if (this.form.password === this.form.checkPass) {
                console.log("发送Ajax请求");
                let result = await req_register({
                    method: "post",
                    url: "/register",
                    arg: {
                        user_name: this.form.name,
                        password: this.form.password,
                    },
                    headerType: "form",
                });
                console.log(result);
                if (result.data == '注册成功！！') {
                    this.$router.push({
                        name: 'login'
                    })
                }


            } else {
                console.log("密码不一致，请重新输入");
                // 弹框  要引入import { Message } from "element-ui";
                Message({
                    type: 'warning',
                    message: '请重新输入密码',
                });
                // 清空密码框
                this.form.pass == "";
                this.form.checkPass == "";
            }

        },
        onReset() {
            console.log("重置");
            this.form.name = "";
            this.form.pass == "";
            this.form.checkPass == "";
        }

    },
}

</script>

<style lang="scss" scoped>
div {
    display: flex;
    align-items: center;
    justify-content: center;

    .el-form {
        width: 500px;
        height: 300px;
        background-image: url("~@/assets/bjt.webp");
        background-size: 100% 100%;

        .el-input {
            width: 300px;
        }

        .el-form-item {
            height: 60px;
        }
    }
}
</style>